<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学号" prop="stuNo">
        <el-input v-model="queryParams.stuNo" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属班级" prop="classId">
        <el-cascader placeholder="请选择班级" v-model="classVlue1" :show-all-levels="false" :options="ClassNameList"
          @change="handleChange1" clearable filterable>
          <template slot-scope="{ node, data }">
            <span>{{ data.label }}</span>
            <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
          </template>
        </el-cascader>
        <!-- <el-form-item label="性别" prop="gender">
          <el-input v-model="queryParams.gender" placeholder="请输入性别" clearable @keyup.enter.native="handleQuery" />
        </el-form-item> -->
        <!-- <el-form-item label="生日" prop="birthday">
        <el-date-picker clearable v-model="queryParams.birthday" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择生日">
        </el-date-picker>
      </el-form-item> -->
        <!-- <el-form-item label="身份证" prop="idCard">
          <el-input v-model="queryParams.idCard" placeholder="请输入身份证号码" clearable @keyup.enter.native="handleQuery" />
        </el-form-item> -->

      </el-form-item>
      <!-- <el-form-item label="手机号码" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="家庭地址" prop="address">
        <el-input v-model="queryParams.address" placeholder="请输入家庭地址" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-refresh" size="mini" type="success" @click="doInitPwd">初始化密码</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:student:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:student:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:student:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
          v-hasPermi="['system:stuent:import']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:student:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学号" align="center" prop="stuNo" width="150" />
      <el-table-column label="姓名" align="center" prop="name" width="100" />
      <el-table-column label="性别" align="center" prop="gender" />
      <!-- <el-table-column label="生日" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="身份证" align="center" prop="idCard" /> -->
      <el-table-column label="学院名称" align="center" prop="deptId">
        <template slot-scope="scope">
          <span>{{ scope.row.dept && scope.row.dept.deptName !== null ? scope.row.dept.deptName : "暂无学院名称" }}</span>
        </template>
      </el-table-column>

      <el-table-column label="专业名称" align="center" prop="majorId" width="250">
        <template slot-scope="scope">
          <span>{{ scope.row.srsMajors&&scope.row.srsMajors.majorName !== null ? scope.row.srsMajors.majorName : "暂无专业名称"}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="年级id" align="center" prop="gradeId" /> -->
      <el-table-column label="班级名称" align="center" prop="classId" width="270">
        <template slot-scope="scope">
          <span>{{ scope.row.srsClass&&scope.row.srsMajors.className !== null ? scope.row.srsMajors.className : "暂无班级名称" }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="家庭地址" align="center" prop="address" /> -->
      <el-table-column label="学生状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.srs_stu_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:student:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:student:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-refresh" @click="doInitPwdOne(scope.row)">初始化密码</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="picVClick(scope.row)">查看画像</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 添加或修改学生信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="学号" prop="stuNo">
          <el-input v-model="form.stuNo" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-date-picker clearable v-model="form.birthday" type="date" value-format="yyyy-MM-dd" placeholder="请选择生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="身份证号码" prop="idCard" >
          <el-input v-model="form.idCard" placeholder="请输入身份证号码" readonly="true"/>
        </el-form-item>
        <el-form-item label="所属班级" prop="classId">
          <el-cascader placeholder="请选择班级" v-model="classVlue2" :show-all-levels="false" :options="ClassNameList"
            @change="handleChange2" clearable filterable>
            <template slot-scope="{ node, data }">
              <span>{{ data.label }}</span>
              <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
            </template>
          </el-cascader>

        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="家庭地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入家庭地址" />
        </el-form-item>
        <!-- <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的学生数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
            @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="picV" fullscreen>
      <StuPic v-if="picV" :stuNo="pic_stu_no" />
    </el-dialog>

  </div>
</template>

<script>
import { listStudent, getStudent, delStudent, addStudent, updateStudent, getClassName, initPwd, initOnePwd } from "@/api/stuCQS/basedata/student";
// import {  } from "@/api/stuCQS/basedata/class";
import StuPic from "@/views/basedata/private/StuPic";

import { getToken } from "@/utils/auth";
export default {
  name: "Student",
  dicts: ['srs_stu_status'],
  components: {
    StuPic
  },
  data() {
    return {
      pic_stu_no: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生信息表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        stuNo: null,
        gender: null,
        birthday: null,
        idCard: null,
        gradeId: null,
        deptId: null,
        majorId: null,
        classId: null,
        phone: null,
        address: null,
        status: null,
      },
      // 学生导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的学生数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/student/importData"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名字不能为空", trigger: "change" }
        ],
        stuNo: [
          { required: true, message: "学号不能为空", trigger: "change" }
        ],
        gender: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        idCard: [
          { required: true, message: "身份证不能为空", trigger: "change" }
        ], 
        majorId: [
          { required: true, message: "所属班级不能为空", trigger: "change" }
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "change" }
        ],
        address: [
          { required: true, message: "手机号不能为空", trigger: "change" }
        ],
        delFlag: [
          { required: true, message: "删除标志不能为空", trigger: "blur" }
        ]
      },

      ClassNameList: [],//班级名称
      classVlue1: [],//班级搜索选择
      classVlue2: [],//班级添加修改选择

      picV: false
    };
  },
  created() {
    this.getList();
    this.getClassNameList();
  },
  methods: {
    picVClick(row) {
      this.pic_stu_no = row.stuNo;
      this.picV = true;
    },
    async doInitPwdOne(val) {
      let sdata = {
        username: val.stuNo
      }
      let res = await initOnePwd(sdata);
      if (res.code == 200) {
        this.$message.success(res.msg);
      }
    },
    doInitPwd() {
      this.$message.info("正在初始化中，请稍候");
      initPwd().then(res => {
        this.$message.info(res.msg);
      });

    },
    /** 查询学生信息列表 */
    getList() {
      this.loading = true;
      listStudent(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getClassNameList() {
      getClassName().then(res => {
        this.ClassNameList = res.data
      })
    },
    //搜索班级选择
    handleChange1(value) {
      this.queryParams.classId = value[2]
      //console.log(value);
    },
    //班级选择添加修改
    handleChange2(value) {
      this.form.classId = value[2]
      //console.log(value);
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        stuId: null,
        name: null,
        stuNo: null,
        gender: null,
        birthday: null,
        idCard: null,
        deptId: null,
        majorId: null,
        classId: null,
        phone: null,
        address: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.classVlue1 = [],
        this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.stuId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.classVlue2 = [],
        this.open = true;
      this.title = "添加学生信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const stuId = row.stuId || this.ids
      getStudent(stuId).then(response => {
        this.form = response.data;
        var data = response.data;
        this.classVlue2 = [data.dept.deptId, data.srsMajors.majorId, data.srsClass.classId]
        //console.log(this.classVlue2);
        this.open = true;
        this.title = "修改学生信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.stuId != null) {
            updateStudent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const stuIds = row.stuId || this.ids;
      this.$modal.confirm('是否确认删除学生信息编号为"' + stuIds + '"的数据项？').then(function () {
        return delStudent(stuIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "学生导入";
      this.upload.open = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/student/export', {
        ...this.queryParams
      }, `student_${new Date().getTime()}.xlsx`)
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/student/importTemplate', {
      }, `student_info_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>

<style scoped>
.stu_pic {
  margin: 0px;
  background: url("~@/assets/stu_pic/stu.png") no-repeat;
  background-size: 100% 100%;
  width: 100%;
  height: 100vh;
}
</style>